package com.example.sunsg.okhttptest.retrofit;


import com.example.sunsg.okhttptest.TheApplication;
import com.facebook.stetho.okhttp3.StethoInterceptor;
import com.squareup.okhttp.Headers;

import java.io.IOException;
import java.net.HttpCookie;
import java.net.URI;
import java.util.List;
import java.util.concurrent.TimeUnit;

import okhttp3.Cookie;
import okhttp3.CookieJar;
import okhttp3.HttpUrl;
import okhttp3.Interceptor;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import okhttp3.logging.*;
import okhttp3.logging.HttpLoggingInterceptor;

/**
 * Created by NOTE-026 on 2016/8/4.
 */
public class OkHttp3ClientManager {
    public static HttpLoggingInterceptor interceptor;
    static {
        interceptor = new HttpLoggingInterceptor();
        interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
    }
    public static final OkHttpClient mOkHttpClient = new OkHttpClient.Builder()
            .cookieJar(new CookieJar() {
                //cookier
                private final PersistentCookieStore cookieStore = new PersistentCookieStore(TheApplication.getApplication());

                @Override
                public void saveFromResponse(HttpUrl url, List<Cookie> cookies) {
                    if (cookies != null && cookies.size() > 0) {
                        for (Cookie item : cookies) {
                            cookieStore.add(url, item);
//                            token = "Bearer "+item.value();
                        }
                    }
                }

                @Override
                public List<Cookie> loadForRequest(HttpUrl url) {
                    List<Cookie> cookies = cookieStore.get(url);
                    return cookies;
                }
            })
            //超时
            .connectTimeout(20, TimeUnit.SECONDS)
//            .addNetworkInterceptor(new HttpLoggingInterceptor())
            .addInterceptor(new Interceptor() {
                @Override
                public Response intercept(Chain chain) throws IOException {
//                    String agent = "BreadTrip/android/6.1.0/zh (android OS5.0.1) trltechn Map/AutoNavi/v1.4.2 (SM-N9100,samsung)";//breadtrip
                    String agent = "Dalvik/2.1.0 (Linux; U; Android 5.1.1; SM-G9200 Build/LMY47X)";//roobo
                    Request newRequest = chain.request().newBuilder()
                            .addHeader("Authorization",token)
                            .addHeader("User-Agent",agent)
                            .build();
                    return chain.proceed(newRequest);
                }
            })
            .addInterceptor(new PDLogggingInterceptor().setLevel(PDLogggingInterceptor.Level.BODY))
            //添加 StethoInterceptor chrome 浏览器 日至查看
            .addNetworkInterceptor(new StethoInterceptor())
            .build();

//    public static HttpCookie builderHttpCookie(Cookie cookie){
//        HttpCookie httpCookie = new HttpCookie(cookie.name(),cookie.value());
//        httpCookie.setDomain(cookie.domain());
//
//    }

    public static String token = "";

}
